<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== itellico Net Architect ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 08.08.2005
    * @copyright itellico.com 2005
    *
    *********************************************************/

    /***
    * class ajaxPublishNode
    ****
    * This class will piblish a folder
    ****
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 08.08.2005
    * @copyright itellico.com 2005
    ***/
    class ajaxChangeURL extends ajaxAction{

        public function process(){
            $id     = (int)$_REQUEST['id'];
            $type   = (int)$_REQUEST['type'];
            $name   = $_REQUEST['name'];

            //valid URLs out of textual names
            $myTranslit = new translitWrap();

            //some bogus check
            if(empty($name)){
                $name = 'n/a';
            }

            $name = $this->unescape($name);

            //prepare variables
            $pageURL = $myTranslit->UrlTranslit($name); //url of the new node

            //check for a deformed page url
            if(empty($pageURL)){
                $pageURL = 'n_a';
            }
            
            //try to drop the page
            try{
                
                //chaning a folder
                if(!$type){
                    
                    $parent = $this->db->getOne("SELECT parent_id FROM structure WHERE id = ".(int)$id);
                    if($this->db->getOne("SELECT id FROM structure WHERE parent_id = ".(int)$parent." AND url = '".$pageURL."'")){
                         $pageURL .= '_' . (int)$id;
                    }

                    //get rid of the reference to the node
                    $this->db->Execute("UPDATE structure SET url = ".$this->db->qstr($pageURL).", path_cache = NULL WHERE  id = " . $id);
                    $this->db->Execute("SELECT get_path(".$id.")");
                
                    //log action
                    $this->log->recordAction("Ajax: Changed Access URL for Folder (".$id.")");
                }else{
                    $parent = $this->db->getOne("SELECT nodeid FROM pagemap WHERE id = ".(int)$id);
                    if($this->db->getOne("SELECT id FROM pagemap WHERE nodeid = ".(int)$parent." AND pageurl = '".$pageURL."'")){    
                         $pageURL .= '_' . (int)$id;    
                    }

                    //get rid of the reference to the node
                    $this->db->Execute("UPDATE pagemap SET pageurl = ".$this->db->qstr($pageURL)." WHERE  id = " . $id);
                    
                    $pageURL .= '.html';
                    
                    //log action
                    $this->log->recordAction("Ajax: Changed Access URL for Page (".$id.")");
                }
                
            //something went wrong
            }catch(exception $e){
                //log error
                $this->log->recordError($e->getMessage());

                //do nothing
            }
            
            $this->r->html = $pageURL;
        }
    }
?>